package deibay.client.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXFrame;

import deibay.client.core.views.IPanelMain;
import deibay.client.core.views.IView;
import deibay.client.core.views.IViewLogin;
import deibay.client.core.views.IViewRegister;


public class DeibayClientView extends JXFrame implements IView{

	private PanelMainView panel = null;
	private JMenuBar menubar = null;
	private JMenu loginMenu = null;
	private JMenuItem loginItem = null;
	private JMenuItem registerItem = null;
	private JMenuItem logoutItem = null;
	
	public DeibayClientView()
	{
		super();		
		initialize();		
	}
	
	private void initialize() {
		this.setTitle("Deibay Client");
		this.setSize(800, 600);
		this.setJMenuBar(getMenu());
		this.setContentPane(getPanelMain());
		this.setResizable(false);	
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		this.setVisible(true);								
	}

	private PanelMainView getPanelMain()
	{
		if(panel == null)
		{
			panel = new PanelMainView();
		}
		
		return panel;
	}
	
	private JMenuBar getMenu()
	{
		if(menubar == null)
		{
			menubar = new JMenuBar();		
			menubar.setBackground(Color.GRAY);	
			menubar.add(getLoginMenu());
		}
		
		return menubar;
	}
	
	private JMenu getLoginMenu()
	{
		if(loginMenu == null)
		{
			loginMenu = new JMenu("Account");			
			loginMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
			loginMenu.add(getLoginItem());
			loginMenu.add(getRegisterItem());
			loginMenu.add(getLogoutItem());
		}
		return loginMenu;
	}
	
	private JMenuItem getLogoutItem() {		
		if(logoutItem == null)
		{
			logoutItem = new JMenuItem("Logout");
			loginItem.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		return logoutItem;
	}

	private JMenuItem getLoginItem()
	{
		if(loginItem == null)
		{
			loginItem = new JMenuItem("Login");		
			loginItem.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		
		return loginItem;
	}
	
	private JMenuItem getRegisterItem()
	{
		if(registerItem == null)
		{
			registerItem = new JMenuItem("Register");
			registerItem.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		
		return registerItem;
	}
	
	public IPanelMain getPanel() {		
		return (IPanelMain) panel;
	}

	public void setLoginItemAction(ActionListener listener) {		
		loginItem.addActionListener(listener);
	}

	public void setRegisterItemAction(ActionListener listener) {
		registerItem.addActionListener(listener);
	}

	public void setLogoutItemAction(ActionListener listener) {		
		logoutItem.addActionListener(listener);
	}

	public void setLoginItemDisabled() {		
		loginItem.setEnabled(false);
	}

	public void setLoginItemEnabled() {		
		loginItem.setEnabled(true);
	}

	public void setLogoutItemDisabled() {
		logoutItem.setEnabled(false);
	}

	public void setLogoutItemEnabled() {
		logoutItem.setEnabled(true);		
	}

	public void setRegisterItemDisabled() {		
		registerItem.setEnabled(false);
	}

	public void setRegisterItemEnabled() {
		registerItem.setEnabled(true);		
	}

	public void setWindowClose(WindowAdapter adapter) {
		this.addWindowListener(adapter);
	}

	public IViewLogin startLoginView() {
		return new LoginView();
	}

	public IViewRegister startRegisterView() {
		return new RegisterView();
	}
}
